package com.fare.taluo.web.action;




import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Namespace;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.common.remote.ftp.FtpConfig;
import com.common.remote.ftp.FtpTransferException;
import com.common.remote.sftp.SFtpFileTransferClient;
import com.fare.taluo.BaseFdcAction;
import com.fare.taluo.application.ReportDownloadApplication;
import com.fare.taluo.auth.security.ContextLoginUser;
import com.fare.taluo.auth.security.LogonUserCertificate;
import com.fare.util.ReportProperties;

@Namespace("/")
@Controller("reportUploadAction")
@Scope("prototype")
public class ReportUploadAction extends BaseFdcAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = -8919144481200721875L;
	private static final Logger logger = LoggerFactory.getLogger(ReportUploadAction.class);
	private String returnPageURL; 
	private File file;
	
	private String status;
	private String fileName;
	@Resource(name="reportDownloadApplicationImpl")
	private ReportDownloadApplication reportDownloadApplication;
	
	
	private File upload;//上传的文件
	private String uploadFileName;//上传文件的名字
	
	private String miaoshu;//文件的描述
	private int year;//上传报告的年份
	private int month;//上传报告的月份

	
	public String getFileName() {
		
		return this.fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getReturnPageURL() {
		return returnPageURL;
	}

	public void setReturnPageURL(String returnPageURL) {
		this.returnPageURL = returnPageURL;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getMiaoshu() {
		return miaoshu;
	}

	public void setMiaoshu(String miaoshu) {
		this.miaoshu = miaoshu;
	}



	public String input(){
		returnPageURL = "/fare/reportUpload.jsp";
		return "dispatcher";
	}
	
	
	public String upload() throws Exception{
		LogonUserCertificate user = ContextLoginUser.getLoginUser();
		if(user==null){
			throw new Exception("user no login");
		}
		if(this.year==0 || this.month==0){
			throw new Exception("report year or month error");
		}
		if(this.miaoshu==null || this.miaoshu.trim().equals("")){
        	this.miaoshu="暂无说明！";
        }
		//取得配置文件实例
		ReportProperties rp=ReportProperties.getInstance();
		//ftp连接配置
		FtpConfig config=new FtpConfig(rp.getAddress(),rp.getPort(),rp.getUserName(),rp.getPassword());
		SFtpFileTransferClient sftpClient=new SFtpFileTransferClient(config);
		
		boolean flag=false;
		try {
			flag = sftpClient.connect();
		} catch (FtpTransferException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(!flag){
			throw new Exception("connect sftp fail");
		}
		
		InputStream is = null;
		//上传文件的随即名字
		String radName=UUID.randomUUID().toString()+".pdf";
		try {
			is=new FileInputStream(this.getUpload());
			
			//文件目录路径
			String dir=rp.getPath()+this.year+"/"+this.month;
			//一级一级创建目录
			String[] dirs=dir.split("/");
			StringBuffer buffer=new StringBuffer();
			for(String d:dirs){
				buffer.append(d+"/");
				if(!sftpClient.isDirectoryExists(buffer.toString())){
					sftpClient.makeDirectory(buffer.toString());
				}
			}
			sftpClient.upload(is,dir+"/"+radName);
		} catch (Exception e) {
			throw new Exception(e);
		} finally{
			sftpClient.disconnect();
			try {
				if(is!=null){is.close();}
			} catch (Exception e) {}
		}
		
		reportDownloadApplication.saveUploadInfo(this.uploadFileName,
        		this.miaoshu, 
        		this.year+"/"+this.month+"/"+radName, user.getUserName(),
        		this.year,this.month);
        status = "success";
		returnPageURL = "/fare/reportUpload.jsp";
		return "dispatcher";
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	
	
}
